//
//  ST_ZoomInDissmissTransitioning.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/12.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit

class ST_ZoomInDissmissTransitioning:NSObject,UIViewControllerAnimatedTransitioning {
    
    private let snapshot: UIView
    
    init(snapshot: UIView) {
        self.snapshot = snapshot
    }
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.25
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        guard let toVC = transitionContext.viewController(forKey: .to)
            else {
                return
        }
        
        guard let fromVC = transitionContext.viewController(forKey: .from)
            else {
                return
        }
        
        let containerView = transitionContext.containerView
        
        let finalFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: ScreenWidth, height: ScreenHeight))
        
        var header_height:CGFloat = {
               return ScreenWidth/375*600.cgFloat
           }()
        
        //        snapshot.cornerRadius = 5
        snapshot.layer.masksToBounds = true
        
        snapshot.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: ScreenWidth, height: header_height))
        
        containerView.addSubview(toVC.view)
        containerView.addSubview(snapshot)
        toVC.view.isHidden = true
        fromVC.view.isHidden = true
        
        let duration = transitionDuration(using: transitionContext)
        
//        UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: .curveEaseInOut, animations: {
//
//            self.snapshot.frame = finalFrame
//
//        }) { _ in
//
//            toVC.view.isHidden = false
//            self.snapshot.removeFromSuperview()
//            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
//        }
        
        UIView.animate(withDuration: duration, animations: {
            
            self.snapshot.frame = finalFrame
            
            self.snapshot.alpha = 0.2
            
        }) { (f) in
            
            toVC.view.isHidden = false
            self.snapshot.removeFromSuperview()
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            
        }
        
        
    }
    
}
